#include <QThread>
#include "MiniComClient.h"

namespace vkbrd
{
	class miniComClient;

	/**
	* Thread to handle serial port connection (write/read).
	* Able to communicate via Signals (write, report read, close)
	*/
	class QtAsioThread: public QThread
	{
		Q_OBJECT;

	public slots:

		/**
		* Write character to opened serial port.
		*/
		void writeToPort(unsigned char msg);

		/**
		* Order to close connection and end a thread. 
		* Calling thread should wait for AtAsioThread to end, and 
		* delete it after that.
		*/
		void stop();

	private slots:

		/**
		* Self calling slot to handle serial port events in a loop.
		*/
		void pollLoop();

	signals:

		/**
		* Signal sent when serial ports received some bytes of data.
		*/
		void receivedData(unsigned char* read_msg_, int bytes_transferred );

	public:

		/**
		* @param _baudRate Baud rate of serial port.
		* @param _portName "COMX" on Windows machines.
		*/
		QtAsioThread(int _baudRate, std::string& _portName);

	private:

		//! Indicates if reading/writing serial port loop needs to be ended.
		bool terminating;

		//! Com client to communicate
		miniComClient* comClient;

		//! IoService to install handlers.
		boost::asio::io_service ioService; 
	};
}